package fileExplorer;

import java.io.File;
import javax.swing.JOptionPane;

/** 
 *  This class will contain the search algorithm for searching for files and folders. This class will be used for the browse feature of the program.
 */
public class FileAndFolderSearch {
  /* {author=George Siegle}*/

	private String contents[];
	private String folderOrFileSelected;
	private int indexInHistoricRecords;

	/**
	 * Constructor that initializes the index count for the array storing the browsing history of the user.
	 */
	public FileAndFolderSearch() {
	/* {author=George Siegle}*/
		indexInHistoricRecords = -1;
	}
	
	/** 
	 *  Method that searches for folders at a specific location that the user browses to.
	 */
	public void searchForFilesAndFolders() {
	/* {author=George Siegle}*/
  
		try {
			File parentDir = new File(folderOrFileSelected);
			File[] dirContents = parentDir.listFiles();
			contents = new String[dirContents.length];
	  
			for (int index = 0; index < contents.length; index++) {
				contents[index] = dirContents[index].toString();
			}
			indexInHistoricRecords++;
		} catch(NullPointerException e) {
			JOptionPane.showMessageDialog(null, "Folder/File could not be opened.", "Error", JOptionPane.PLAIN_MESSAGE);
		}
	}

	/** 
	 *  Accessor that returns the folder names and file names of the folders and files at a specific location browsed by the user.
	 */
	public String[] getContents() {
	/* {author=George Siegle}*/
		return contents;
	}

	/**
	 *	Accessor that returns the name of the folder/file the user selects in the browse dialog.
	 */
	public String getFolderOrFileSelected() {
	/* {author=George Siegle}*/
		return folderOrFileSelected;
	}
  
	/**
	 * Method that returns the previously selected folder folder which is the parent folder.
	 */
	public String getPreviousSelectedFolder() {
	/* {author=George Siegle}*/
		String[] filePath = folderOrFileSelected.split("\\\\");
		String backDir = new String();
		for (int index = 0; index < filePath.length - 1; index++) {
			if (index <= filePath.length - 2) {
				backDir += filePath[index] + "\\";
			}
			else {
				backDir += filePath[index];
			}
		}
		indexInHistoricRecords -= 2;
		return backDir;
	}
	
	/** 
	 *  Mutator that sets the folderSelected variable to the folder name that the user selected through the browse window.
	 */
	public void setFolderOrFileSelected(String folderSelected) {
	/* {author=George Siegle}*/
		this.folderOrFileSelected = folderSelected;
	}
  
	/**
	 * Accessor that returns the current index of the file tree.
	 */
	public int getIndexInHistoricRecords() {
	/* {author=George Siegle}*/
		return indexInHistoricRecords;
	}
  
	/**
	 * Returns a boolean value corresponding to whether the item selected by the user is a file or folder.
	 */
	public boolean isFile() {
	/* {author=George Siegle}*/
		if (new File(folderOrFileSelected).isFile()) {
			return true;
		}
		else {
			return false;
		}
	}
}